package com.itheima.sh.Session;

import com.itheima.sh.Util.ServletUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/historyServlet")
public class historyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("text/html;charset=utf-8");
        Cookie history = ServletUtil.getCookie(request, "history");
        if (history==null){
            response.getWriter().print("没有浏览纪录");
            response.getWriter().print("<a href=\"/goods.html\"> 去浏览 </a>");

        }else {
            String value = history.getValue();
            String[] split = value.split("-");
            List<String> list = Arrays.asList(split);
            for (String s : list) {
                response.getWriter().println(s);
            }

            response.getWriter().print("<br/><a href=\"/goods.html\"> 继续浏览 </a>");
        }
    }
}
